<?php
namespace app\api\controller;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods:POST,GET");

use app\common\controller\HomeBase;
use app\api\controller\Solutions;
use app\common\model\Nav     as NavModel;
use app\common\model\NavNews as NavNewsModel;
use app\common\model\NavAnli as NavAnliModel;
use think\Db;

/**
 * 案例接口
 * Class Index
 */
class Anli extends HomeBase
{
    protected function _initialize(){
        parent::_initialize();
        $this->nav_model = new NavModel();
        $this->nav_news  = new NavNewsModel();
        $this->nav_anli  = new NavAnliModel();
        $this->solutions = new Solutions();
    }

	/**
    *案例显示
    *@return json
    **/
    public function show()
    {
        $nav_id = $this->request->param('nav_id');
        $this->request->param('limit') ? $limit = $this->request->param('limit'): $limit = 8;
        $this->request->param('page')  ? $page = $this->request->param('page') : $page = 1;
        //判断传过来的有没有传导航栏id
        if($nav_id){
            //有pid
            $list3 = $this->nav_anli->where('nav_id',$nav_id)->where('status',1)->select();
            $list1 = $this->nav_anli->where('nav_id',$nav_id)->where('status',1)->order('create_time desc,top desc')->paginate($limit, false, ['page' => $page]);
            $list1 = $list1->toArray();
        }else{
            //没有传导航栏id 首页的时候和全部案例的时候
            $list3 = $this->nav_anli->where('status',1)->select();
            $list1 = $this->nav_anli->where('status',1)->order('create_time desc,top desc')->paginate($limit, false, ['page' => $page]);
            $list1 = $list1->toArray();
        }
        if($list1){
            $listinfo = getlistphoto($list1['data']);
            $total = count($list3);
            $info  = [
                    'list' => $listinfo,
                    'total' => $total,
                    'page' => $page,
                ];
             $this->apiSuccess($info);
        }else{
            $this->apiError('没有查到信息');
        }
    }

    /**
    *单个详情方案显示
    *@return json
    **/
    public function showDetail()
    {
        $id      = $this->request->param('id');
        $detail  = $this->nav_anli->where('id',$id)->select();
        if($detail){
            //获得上一个下一个题目
            $top  = $this->solutions->getUpDownTitle($id,'nav_anli',1);
            $down = $this->solutions->getUpDownTitle($id,'nav_anli',0);
            //将内容格式化
            $detail[0]['content'] = htmlspecialchars_decode($detail[0]['content']);
                $list = [
                    'list' =>$detail[0],
                    'top'  =>$top,
                    'down' =>$down,
                ];
                $this->apiSuccess($list);
        }else{
            $this->apiError('没有查到信息');
        }
    }

    /**
    *案例的上一篇下一篇
    *@return json
    **/

    public function showUpDown()
    {
        $id  = $this->request->param('id');
        $top = $this->request->param('top');
        $info = $this->solutions->getUpDownId($id,"nav_anli",$top);
            if (is_int($info)) {
                //上一篇下一篇文章标题
                $top  = $this->solutions->getUpDownTitle($info,'nav_anli',1);
                $down = $this->solutions->getUpDownTitle($info,'nav_anli',0);

                $detail  = $this->nav_anli->where('id',$info)->select();
                if($detail){
                    $detail1 = getlistphoto($detail);
                    //将内容格式化
                    $detail1[0]['content'] = htmlspecialchars_decode($detail1[0]['content']);
                    $listInfo = [
                        'list'=>$detail[0],
                        'top'=>$top,
                        'down'=>$down,
                    ];
                    $this->apiSuccess($listInfo);
                }else{
                    $this->apiError('没有查到信息');
                }
            }else{
                $this->apiError('没有查到信息');
            }
    }

    /**
    *置顶的anli显示
    *@return json
    **/
    public function topanlishow()
    {
        $id  = $this->request->param('nav_id');
        $this->request->param('limit') ? $limit = $this->request->param('limit') : $limit = 6;
        if($id){
            $list = $this->nav_anli->where('nav_id',$id)->where('status',1)->where('top',1)->order('create_time',' desc')->limit($limit)->select();
        }else{
            $list = $this->nav_anli->where('status',1)->where('top',1)->order('create_time',' desc')->limit($limit)->select();
        }
        if($list){
            $this->apiSuccess($list);
        }else{
            $this->apiError('没有查到信息');
        }
        
    }
}